home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / dev / lang / amigatalk.lha / general / Dictionary.st < prev    next >
Text File  |  2001-12-11  |  3KB  |  99 lines

  1. "--------------------------------------------------------------"
  2. "  Dictionaries are implemented using Points in order to reduce
  3.    the number of classes in the standard prelude
  4.    This also has the advantage of making the output appear in
  5.       'key @ value'    form"
  6. "--------------------------------------------------------------"
  7.  
  8. Class Dictionary :KeyedCollection
  9. ! hashTable currentBucket currentList !
  10. [
  11.    new
  12.       hashTable <- Array new: 17
  13. |
  14.    hashNumber: aKey
  15.       ^ (<primitive 5 aKey> intNegRem: hashTable size) + 1
  16. |
  17.    getList: aKey      ! list bucketNumber !
  18.       bucketNumber <- self hashNumber: aKey.
  19.       list         <- hashTable at: bucketNumber.
  20.  
  21.       (list isNil)
  22.          ifTrue: [list <- List new.
  23.                   hashTable at: bucketNumber put: list
  24.                  ].
  25.       ^ list
  26. |
  27.    at: aKey put: anObject   ! list anAssoc !
  28.       list    <- self getList: aKey.
  29.       anAssoc <- self findAssociation: aKey inList: list.
  30.  
  31.       (anAssoc isNil)
  32.          ifTrue:  [anAssoc <- (Point new x: aKey) y: anObject.
  33.                    list add: anAssoc
  34.                   ]
  35.          ifFalse: [anAssoc y: anObject].
  36.  
  37.       ^ anObject
  38. |
  39.    at: aKey ifAbsent: exceptionBlock   ! list anAssoc ! 
  40.       list    <- self getList: aKey.
  41.  
  42.       anAssoc <- self findAssociation: aKey inList: list.
  43.  
  44.       (anAssoc isNil)
  45.          ifTrue: [^ exceptionBlock value].
  46.  
  47.       ^ anAssoc y
  48. |
  49.    removeKey: aKey ifAbsent: exceptionBlock    ! list anAssoc !
  50.       list    <- self getList: aKey.
  51.       anAssoc <- self findAssociation: aKey inList: list.
  52.  
  53.       (anAssoc isNil)
  54.          ifTrue: [^ exceptionBlock value].
  55.  
  56.       ^ ( list remove: anAssoc 
  57.           ifAbsent: [ ^ exceptionBlock value ] ) y
  58. |
  59.    findAssociation: aKey inList: linkedList
  60.       linkedList do: [:item | (item x = aKey) ifTrue: [^ item] ].
  61.  
  62.       ^ nil
  63. |
  64.    first ! item !
  65.       (1 to: 17) do: 
  66.          [:i | ((item <- self checkBucket: i) notNil)
  67.              ifTrue: [ ^ item y] ] .
  68.       ^ nil
  69. |
  70.    next         ! item !
  71.       ((item <- currentList next) notNil)
  72.          ifTrue: [ ^ item y ].
  73.  
  74.       [currentBucket < 17] whileTrue:
  75.          [currentBucket <- currentBucket + 1.
  76.           ((item <- self checkBucket: currentBucket) notNil)
  77.             ifTrue: [ ^ item y ] ].
  78.       ^ nil
  79. |
  80.    printString
  81.       ^ (self inject: (self class printString) , ' ( '
  82.                 into: [ :aString :aValue |
  83.                         aString , self currentKey printString ,
  84.                         ' @ ' , aValue printString , ' ' ]
  85.                 ) , ')'
  86. |
  87.    currentKey   ! clist !
  88.       ^ (currentList notNil) 
  89.          ifTrue: [clist <- currentList current.
  90.              (clist notNil) ifTrue: [clist x]
  91.                  ]
  92. |
  93.    checkBucket: bucketNumber
  94.      ((currentList <- hashTable at: (currentBucket <- bucketNumber)) isNil)
  95.        ifTrue: [ ^ nil ].
  96.  
  97.      ^ currentList first
  98. ]
  99.